class CustomersController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer
  
  def index
    @customers = Customer.find(:all, :order => 'name')
    respond_to do |format|
      format.html {render :action => 'list.rhtml'}
      format.js {render :action => 'list.rjs'}
      format.xml {render :xml => @customers.to_xml}
    end
  end
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :put, :only => [ :update ], :redirect_to => { :action => :index }
  verify :method => :post, :only => [ :create ], :redirect_to => { :action => :index }
  verify :method => :delete, :only => [ :destroy ], :redirect_to => { :action => :index }
  
  def search
    @search = Search.new(params[:search])
    if @search.query.nil?
      if @search.tag.nil?
        @customers = Customer.find(:all, :order => 'name')
      else
        tag = Tag.find(:first, :conditions => ["name = ?", @search.tag])
        if !tag.nil?
          @customers = tag.customers
        end
      end
    else
      if @search.tag.nil?

        @customers = Customer.find(:all, :conditions => ["name like ?", "%#{@search.query}%"], :order => 'name')
      else
        tag = Tag.find(:first, :conditions => ["name = ?", @search.tag])
        if !tag.nil?
          @customers = tag.customers.find(:all, :conditions => ["name like ?", "%#{@search.query}%"], :order => 'name')
        end
      end 
    end
   
    respond_to do |format|
      format.html {render :action => 'list.rhtml'}
      format.js {render :action => 'list.rjs'}
    end
  end
  
  def show
    if(!params[:id].nil?)
      @customer = Customer.find(params[:id])
    elsif (!@conversation.nil?)
      @customer = @conversation.customer
    end
    @conversations = @customer.conversations
    @notes = @customer.notes
    respond_to do |format|
      format.html {render :action => 'show.rhtml'}
      format.js {render :action => 'show.rjs'}
      format.xml {render :xml => @customer.to_xml(:include => [:contacts, :conversations, :tags, :notes, :addresses])}
    end
  end
  
  def new
    @customer = Customer.new
    respond_to do |format|
      format.html {render :action => 'new.rhtml'}
      format.js {render :action => 'new.rjs'}
    end
  end
  
  def create
    @customer = Customer.new(params[:customer])
    if @customer.save
      update_tags(@customer)
      index
    else
      respond_to do |format|
        format.html {render :action => 'new.rhtml'}
        format.js {render :action => 'new.rjs'}
      end
    end
  end
  
  def edit
    @customer = Customer.find(params[:id])
    @tags = Tag.find(:all) - @customer.tags
    respond_to do |format|
      format.html {render :action => 'edit.rhtml'}
      format.js {render :action => 'edit.rjs'}
    end
  end
  
  def update
    @customer = Customer.find(params[:id])
    if @customer.update_attributes(params[:customer])
      update_tags(@customer)
      @customer = Customer.find(params[:id])
      #redirect_to :action => 'show' unless request.xhr?
    else
      respond_to do |format|
        format.html {render :action => 'edit.rhtml'}
        format.js {render :action => 'edit.rjs'}
      end
    end 
  end
  
  def destroy_selected
    unless params[:selected].nil?
      params[:selected].each { |id| Customer.find(id).destroy }
    end
    respond_to do |format|
      format.html {redirect_to :action => 'list'}
      format.js do
        @customers = Customer.find(:all, :order => 'name')
        render :action => 'list.rjs'
      end
    end
  end
  
  def destroy
    Customer.find(params[:id]).destroy
    redirect_to :action => 'list' unless request.xhr?
  end
   
  def add_tag
    @customer = Customer.find(params[:id])
    tag = Tag.find(params[:tag][:tag_id])
    @customer.tags << tag
    @tags = Tag.find(:all) - @customer.tags
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'tags.js.rjs'}
    end
  end
  
  def remove_tag
    @customer = Customer.find(params[:id])
    tag = Tag.find(params[:tag])
    @customer.tags.delete(tag)
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'tags.js.rjs'}
    end
  end

  private

  def update_tags(customer)
    tags_to_remove = Array.new + customer.tags
    if !params[:customer_tags].nil?
      params[:customer_tags].each { |tag_id|
        tag = Tag.find(tag_id)
        tags_to_remove.delete(tag)
        if !customer.tags.include?(tag)
          customer.tags << tag
        end
      }
      tags_to_remove.each { |tag|
        CustomerTag.delete_all("customer_id = " + customer.id.to_s + " and tag_id = " + tag.id.to_s)
      }
    else
      CustomerTag.delete_all("customer_id = " + customer.id.to_s)
    end
  end

end
